//
//  LPPSettingCell.m
//  Lottery
//
//  Created by 刘盼 on 16/1/23.
//  Copyright © 2016年 lhheh. All rights reserved.
//

#import "LPPSettingCell.h"

@implementation LPPSettingCell

+(instancetype)settingCellWithtableView:(UITableView *)tableView withDictionary:(NSDictionary *)item {
    
    NSString *settingId = @"setting";
    
    LPPSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:settingId];
    
    if (!cell) {
        
        cell = [[LPPSettingCell alloc]initWithStyle:[self cellStyleWithItem:item] reuseIdentifier:settingId];
    }
    cell.item = item;
    
    return cell;
}
//设置初始化cell的类型
+(UITableViewCellStyle)cellStyleWithItem:(NSDictionary *)item {
    
    if ([item[@"cellType"] isEqualToString:@"UITableViewCellStyleValue1"]) {
        
        return UITableViewCellStyleValue1;
        
    }else if ([item[@"cellType"] isEqualToString:@"UITableViewCellStyleValue2"]){
        return UITableViewCellStyleValue2;
        
    }else if ([item[@"cellType"] isEqualToString:@"UITableViewCellStyleSubtitle"]){
        
        return UITableViewCellStyleSubtitle;
        
    }else{
        
        return UITableViewCellStyleDefault;
    }
}
//传值
-(void)setItem:(NSDictionary *)item{
    
    _item = item;
    
    if (item[@"icon"] && [item[@"icon"]length] > 0) {
        
        self.imageView.image = [UIImage imageNamed:item[@"icon"]];
    }
    self.textLabel.text = item[@"title"];
    
    self.detailTextLabel.text = item[@"subTitle"];
    
    if (item[@"isRed"] && [item[@"isRed"]boolValue]) {
        
        self.detailTextLabel.textColor = [UIColor redColor];
    }
    
    if ([item[@"accessoryType"]isEqualToString:@"UIImageView"]) {
        
        self.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:item[@"accessoryContent"]]];
        
    }else if([item[@"accessoryType"]isEqualToString:@"UISwitch"]){
        
        UISwitch *switchs = [[UISwitch alloc]init];
        
        [switchs addTarget:self action:@selector(switchsChange:) forControlEvents:UIControlEventValueChanged];
        //取出switch状态
        switchs.on = [[NSUserDefaults standardUserDefaults]boolForKey:self.item[@"switch_key"]];
        
        self.accessoryView = switchs;
    }
}

#pragma mark--------switch点击----------

-(void)switchsChange:(UISwitch *)sender {
    //保存switch的状态
    [[NSUserDefaults standardUserDefaults]setBool:sender.isOn forKey:self.item[@"switch_key"]];
    
    [[NSUserDefaults standardUserDefaults]synchronize];
}
@end
